package com.yf.login;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.test.AndroidTestCase;
import android.util.Log;
import android.util.Xml;


public class XmlParseTest extends AndroidTestCase {

	public void testParseXmlByPull() throws IOException, XmlPullParserException {
		
		Reader xmlReader = new FileReader("data/data/com.yf.helloandroid/bookstore.xml");
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(xmlReader);
		int eventType = parser.getEventType();
		List<Book> bookList = null;
		Book book = null;
		
		while(eventType != XmlPullParser.END_DOCUMENT) {
			switch(eventType) {
			case XmlPullParser.START_DOCUMENT:
				bookList = new ArrayList<Book>();
				break;
			case XmlPullParser.START_TAG:
				String name = parser.getName();
				
				if ("book".equals(name)) {
					book = new Book();
				} else if ("title".equals(name)) {
					book.setTitle(parser.nextText());
				}else if ("author".equals(name)) {
					book.setAuthor(parser.nextText());
				}else if ("year".equals(name)) {
					book.setYear(parser.nextText());
				}else if ("price".equals(name)) {
					book.setPrice(parser.nextText());
				}
				break;
			case XmlPullParser.END_TAG:
				if (book != null && "book".equals(parser.getName())) {
					bookList.add(book);
					book = null;
				}
				break;
			}
			
			eventType = parser.next();
		}
		
		xmlReader.close();
		Log.v("XmlParseTest", bookList.toString());
	}
}
